import request from '../../../utils/request.js'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isMember: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '智能急救培训',
      path: '/pages/startup/startup'
    }
  },

  bindRegister() {
    wx.navigateTo({
      url: '/pages/register/register',
    })
  },

  bindLink(e) {
    if (!e.currentTarget.dataset.url) {
      return false;
    }
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },

  bindToggle(e) {
    const index = e.currentTarget.dataset.index
    this.setData({
      isMember: index === '0'
    })
  },

  bindSubmit(e) {
    if(!e.detail.value.username) {
      this.showError('请输入账号')
      return false
    }
    if (!e.detail.value.password) {
      this.showError('请输入密码')
      return false
    }
    request.post('/api/weChatMini/v1/user/login', {
      mobile: e.detail.value.username,
      password: e.detail.value.password
    }).then(res => {
      wx.setStorageSync('expireTime', (new Date()).getTime() + (res.data.expire * 1000))
      wx.setStorageSync('token', res.data.token)
      wx.setStorageSync('userId', res.data.userId)
      wx.setStorageSync('userType', res.data.userType)
      wx.redirectTo({
        url: '/pages/index/index',
      })
    })
  },

  showError(str) {
    wx.showModal({
      title: '提示',
      content: str,
      showCancel: false
    })
  },

  bindScan() {
    wx.scanCode({
      onlyFromCamera: true,
      success: res => {
        console.log(res)
      },
      // fail: err => {
      //   wx.showModal({
      //     title: '提示',
      //     content: '扫码失败',
      //     showCancel: false
      //   })
      // }
    })
  }
})